define(["../layers/GraphicsLayer", "../others/EventDrive", "../symbols/TextSymbol", "../symbols/PrimitiveSymbol", "../Graphic", "../_Color", "../../Source/Scene/LabelStyle", "../../Source/Scene/HorizontalOrigin", "../../Source/Scene/VerticalOrigin", "../../Source/Core/PolygonGeometry", "../../Source/Core/PolygonHierarchy", "../../Source/Core/Cartesian3", "../../Source/Core/defaultValue"], (function (e, t, i, s, o, r, l, h, n, a, c, v, p) {
  function u(i) {
    this.viewer = i.viewer, this.actis = i.actis, this.viewer.scene.globe.depthTestAgainstTerrain = !0, this.layer = new e({
      viewer: this.viewer
    }), this.circles = [], this.path = [], this.height = [], this.lines = null, this.label = [], this.point = this.viewer.entities.add({
      point: {
        color: r("red"),
        pixelSize: 10
      }
    }), this.labelTotle = this.viewer.entities.add({
      label: {
        show: !1,
        showBackground: !0,
        font: "36px Helvetica",
        fillColor: r("WHITE"),
        outlineColor: r("BACK"),
        outlineWidth: 5,
        style: l.FILL_AND_OUTLINE,
        horizontalOrigin: h.CENTER,
        verticalOrigin: n.BASELINE,
        disableDepthTestDistance: 1e4,
        scale: .5
      }
    }), this.event = new t({
      viewer: this.viewer,
      eventType: "left_click",
      callBack: e => {
        let t = this.path,
          r = this.movePoint.mapPoint.longitude,
          l = this.movePoint.mapPoint.latitude,
          h = this.movePoint.mapPoint.height + 150;
        if (h < -50) return;
        t.push(r, l), this.height.push(h);
        let n = t.length,
          u = new o({
            symbol: {
              type: "point-3d",
              style: {
                type: "cone",
                height: 60,
                width: 30,
                color: "#4af99d"
              }
            },
            geometry: {
              type: "point",
              longitude: r,
              latitude: l,
              height: h + 30
            }
          }),
          d = new s({
            type: "circle",
            center: [r, l],
            radius: .5,
            color: "#4af99d",
            class: "both"
          }),
          w = new s({
            type: t.length > 4 ? "" : "line",
            ranges: t,
            color: "rgba(255, 0, 0, .8)",
            width: 1,
            class: "both"
          });
        if (this.layer.add(u), this.lines && this.viewer.map.remove(this.lines), this.lines = w, this.circles.push(d), this.viewer.map.add(w), this.viewer.map.add(d), n >= 6) {
          var y = new a({
              polygonHierarchy: new c(v.fromDegreesArray(t)),
              perPositionHeight: !0
            }),
            m = a.createGeometry(y);
          if (null != m) {
            for (var g = m.indices, b = m.attributes.position.values, f = [], T = 0, S = 0; S < b.length / 3; S++) {
              var P = new v(0, 0, 0);
              P.x = b[3 * S], P.y = b[3 * S + 1], P.z = b[3 * S + 2], f.push(P)
            }
            for (S = 0; S < g.length / 3; S++) {
              var C = [];
              C.push(f[g[3 * S]], f[g[3 * S + 1]], f[g[3 * S + 2]]);
              var E = v.distance(C[0], C[1]),
                x = v.distance(C[0], C[2]),
                L = v.distance(C[1], C[2]),
                z = (E + x + L) / 2;
              T += Math.sqrt(z * (z - E) * (z - L) * (z - x))
            }
            let e = this.layer.items.map((function (e, t, i) {
              return e.position._value
            }));
            var A = 0,
              D = 0,
              H = 0;
            for (S = 0; S < e.length; S++) A += e[S].x, D += e[S].y, H += e[S].z;
            var _ = e.length,
              B = new v.fromDegrees(0, 0, 0);
            B.x = A / _, B.y = D / _, B.z = H / _, this.labelTotle.position = B, this.labelTotle.show = p(i.showLabel, !0), this.labelTotle.label.show = !0, this.labelTotle.label.text = "总面积：" + T.toFixed(2) + " m²"
            this.actis(this.labelTotle)
          }
        }
        this.viewer.scene.requestRenderMode = !1
      }
    }), this.mouseMove = new t({
      eventType: "mouse_move",
      viewer: i.viewer,
      callBack: e => {
        this.movePoint = e, this.point && (this.point.position = e.cartesian)
      }
    })
  }
  return u.prototype.destroy = function () {
    this.viewer.entities.remove(this.labelTotle), this.viewer.entities.remove(this.point), this.label.forEach(e => this.viewer.entities.remove(e)), this.circles.forEach(e => this.viewer.map.remove(e)), this.viewer.map.remove(this.lines), this.event.destroy(), this.mouseMove.destroy(), this.viewer.scene.globe.depthTestAgainstTerrain = !1, this.height = [], this.circles = [], this.point = null, this.path = [], this.lines = null
  }, u.prototype.getPath = function () {
    return this.path
  }, u
}));
